package com.glowdayz.glowmee.bc;

import java.io.IOException;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.glowdayz.common.DBHandler;
import com.glowdayz.common.PageHandler;
import com.glowdayz.glowmee.dao.PouchCategoryDAO;
import com.glowdayz.glowmee.dao.PouchDAO;

public class TodayPouchBC extends HttpServlet {
	
	private static final long serialVersionUID = 1L;
       
    public TodayPouchBC() {
        super();
    }

	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		DBHandler.getInstance().connect();
		
		request.setCharacterEncoding("utf-8");
		
		String id_str = request.getParameter("id");
		String isToday_str = request.getParameter("isToday");
		if (id_str != null && isToday_str != null) {
			int id = Integer.parseInt(id_str);
			int isToday = Integer.parseInt(isToday_str);
			
			if (isToday == 1) {
				isToday = 0;
			} else if (isToday == 0) {
				isToday = 1;
			}
			
			try {
				PouchDAO.getInstance().isTodayPouch(id, isToday);
				request.setAttribute("alert", "<script type='text/javascript'>alert('처리가 완료되었습니다.')</script>");
			} catch (Exception e) {
				DBHandler.getInstance().disconnect();
				e.printStackTrace();
			}
		}
		
		String page_str = request.getParameter("page");
		int page = 0; 
		if (page_str == null) {
			page = 1;
		} else {
			page = Integer.parseInt(page_str);
		}
		
		try {
			request.setAttribute("pouchCategory", PouchCategoryDAO.getInstance().getPouchCategoryList(1, -1, "pouchCategoryText"));
			request.setAttribute("page", PageHandler.getInstance());
			request.setAttribute("todayPouch", PouchDAO.getInstance().getTodayPouchList(page, 10, null));
		} catch (Exception e) {
			e.printStackTrace();
		}
		
		DBHandler.getInstance().disconnect();
		RequestDispatcher dpr = request.getRequestDispatcher("/pouch/todayPouch.jsp");
		dpr.forward(request, response);
	}

	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		
	}

}